﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using ebay.Dal.CenterDB_SystemTableAdapters;

/// <summary>
///WebUtility 的摘要说明
/// </summary>
public class WebUtility
{
    public WebUtility()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static string InputText(string text, int maxLength)
    {
        text = text.Trim();
        if (string.IsNullOrEmpty(text))
            return string.Empty;
        if (text.Length > maxLength)
            text = text.Substring(0, maxLength);
        text = Regex.Replace(text, "[\\s]{2,}", " ");	//two or more spaces
        text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n");	//<br>
        text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " ");	//&nbsp;
        text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty);	//any other tags
        text = text.Replace("'", "''");
        return text;
    }

    public static bool CheckLogin()
    {
        if (HttpContext.Current.Session["UserId"] == null)
        {
            return true;
        }
        return false;
    }

    public static void CheckRight()
    {
        LoadingCheck(null);
    }
    public static void LoadingCheck(string Url)
    {
        if (HttpContext.Current.Session["UserId"] == null)
        {
            string rootpath = HttpContext.Current.Request.ApplicationPath;
            HttpContext.Current.Response.Write("<script>alert('请先登陆!');window.location.href='" + rootpath + "/login.aspx'</script>");
        }
        else
        {
            string userId = HttpContext.Current.Session["UserId"].ToString();
            string checkUrl = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath;

            if (string.IsNullOrEmpty(Url))
            {
                checkUrl = checkUrl.Substring(2, checkUrl.Length - 2);
            }
            else
            {
                checkUrl = Url;
            }

            T_UserRightTableAdapter adapter = new T_UserRightTableAdapter();
            if (Convert.ToInt32(adapter.IsExistsRight(userId, checkUrl)) <= 0)
            {
                HttpContext.Current.Response.Write("<script>alert('您无此权限!');window.history.go(-1);</script>");
            }
        }
    }

    public static void Quit()
    {
        HttpContext.Current.Session.RemoveAll();
        string rootpath = HttpContext.Current.Request.ApplicationPath;
        HttpContext.Current.Response.Redirect(rootpath + "/login.aspx");
    }

    public static void ShowMsg(string Msg, string href, System.Web.UI.Page page)
    {
        string jstring = "<script>alert('" + Msg + "');";
        if (!string.IsNullOrEmpty(href))
        {
            string root = HttpContext.Current.Request.ApplicationPath;
            jstring += "window.location.href='" + root + href + "'";
        }
        jstring += "</script>";

        page.ClientScript.RegisterStartupScript(page.GetType(), "", jstring);

    }
    public static void ShowMsg(string Msg, System.Web.UI.Page page)
    {
        ShowMsg(Msg, null, page);
    }
}
